/**
 * 订单控制器
 */
var OrderController = (function () {
    function OrderController(context, orderServices, orderEvent) {
        var _this = this;
        this.orderServices = orderServices;
        this.orderServices.orderEvent = orderEvent;
        this._context = context;

        $(this._context).on('click', function (e) {
            e = e || window.event;
            var target = e.target, $_target = $(target);
            //在线支付
            if ($_target.hasClass('onlile-payment')) {
               var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.onlinePayment(data);
            }//取消订单
            else if ($_target.hasClass('cancel-order')) {
                var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.cancelOrder($_target, data);
            }//货到付款
            else if ($_target.hasClass('cash-on-delivery')) {
                var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.cashOnDelivery($('.cash-on-delivery'), data);
            }//确认收货
            else if ($_target.hasClass('wait-buyer-confirm-goods')){
                var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.confirmReceive($('.wait-buyer-confirm-goods'), data);
            }//评价
            else if($_target.hasClass('order-comment')){
                var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.comment($('.order-comment'), data);
            }//删除订单
            else if ($_target.hasClass('delete-order')) {
                var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.deleteOrder($_target, data);
            }//申请退款
            else if($_target.hasClass('order-refund')){
                var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.refund($_target, data);
            }//退款详情
            else if($_target.hasClass('order-refund-detail')){
                var data = eval('('+$_target.attr('data')+')');
                _this.orderServices.refundDetail($_target, data);
            }
        });
    }
    return OrderController;
})();

/**
 * 订单列表页操作后DOM事件
 */
var OrderListEvent = (function () {

    function OrderListEvent() {
        this._delete = $("<a class='delete-order' href='javascript:;'>删除</a>");
        this._comment = $("<a class='order-comment' href='javascript:;'>评价</a>");
        this._detail = $("<a target='_blank'>订单详情</a>");
        this._refund = $("<a target='_blank' class='order-refund' href='javascript:;'>申请退款</a>");
        this._refundDetail = $("<a target='_blank' class='order-refund-detail' href='javascript:;'>退款详情</a>");
    }

    /**
     * 取消
     * @param context
     * @param order
     */
    OrderListEvent.prototype.cancel = function(context, order){
        //更改订单状态，交易关闭
        context.parent().prev().html('交易关闭');
        //添加删除操作
        this._delete.attr('data', JSON.stringify(order));
        this._detail.attr('href', base + "/order/detail.jhtml?orderNo="+order.orderNo);
        context.parent().html(this._delete.prop("outerHTML")+"<br/>"+this._detail.prop("outerHTML"));
    };

    /**
     * 删除
     * @param context
     * @param order
     */
    OrderListEvent.prototype.del = function(context){
        //删除节点
        context.closest('tr').prev().remove();
        context.closest('tr').remove();
    };

    /**
     * 确认收货
     * @param context
     * @param order
     */
    OrderListEvent.prototype.confirmReceive = function(context, order){
        //更改订单状态，交易完成
        context.parent().prev().html('交易完成');
        //添加评价、删除操作
        this._delete.attr('data', JSON.stringify(order));
        this._comment.attr('data', JSON.stringify(order));
        var refundHtml = '<br/>';
        if(order.hasRefund){
            refundHtml = refundHtml + this._refundDetail.attr('data', JSON.stringify(order)).prop("outerHTML");
        }else{
            refundHtml = refundHtml + this._refund.attr('data', JSON.stringify(order)).prop("outerHTML");
        }

        this._detail.attr('href', base + "/order/detail.jhtml?orderNo="+order.orderNo);
        context.parent().html(this._comment.prop("outerHTML")+" | "+this._delete.prop("outerHTML")
        +refundHtml+ "<br/>"+this._detail.prop("outerHTML"));

    };

    return OrderListEvent;
})();

/**
 * 订单详情页操作后DOM事件
 */
var OrderDetailEvent = (function () {

    function OrderDetailEvent() {
        this._delete = $("<a class='button delete-order' href='javascript:;'>删除</a>");
        this._comment = $("<a class='button button-highlight order-comment'>评价</a>");
        this._refund = $("<a class='button order-refund' href='javascript:;'>申请退款</a>");
        this._refundDetail = $("<a class='button order-refund-detail' href='javascript:;'>退款详情</a>");
    }

    /**
     * 取消
     * @param context
     * @param order
     */
    OrderDetailEvent.prototype.cancel = function(context, order){
        //更改订单状态，交易关闭
        context.parent().prev().html('交易关闭');
        //添加删除操作
        this._delete.attr('data', JSON.stringify(order));
        context.parent().html(this._delete.prop("outerHTML"));
    };

    /**
     * 删除
     * @param context
     * @param order
     */
    OrderDetailEvent.prototype.del = function(context){
        //删除节点
        location.href= base + "/order/list.jhtml";
    };

    /**
     * 确认收货
     * @param context
     * @param order
     */
    OrderDetailEvent.prototype.confirmReceive = function(context, order){
        //更改订单状态，交易完成
        context.parent().prev().html('交易完成');
        //添加评价、删除操作
        this._delete.attr('data', JSON.stringify(order));
        this._comment.attr('data', JSON.stringify(order));
        var refundHtml = '';
        if(order.hasRefund){
            refundHtml = refundHtml + this._refundDetail.attr('data', JSON.stringify(order)).prop("outerHTML");
        }else{
            refundHtml = refundHtml + this._refund.attr('data', JSON.stringify(order)).prop("outerHTML");
        }
        context.parent().html(this._comment.prop("outerHTML")+" "+refundHtml+" "+this._delete.prop("outerHTML"));


    };

    return OrderDetailEvent;
})();

$(function () {

    var oOrder = new OrderModule.Order(oDialog);
    oOrder.onlinePaymentUrl = base + '/order/pay.jhtml'; //在线支付
    oOrder.confirmReceiveUrl = base + '/order/confirmReceive.jhtml'; //确认收货
    oOrder.cancelOrderUrl = base + '/order/cancel.jhtml'; //取消订单
    oOrder.deleteOrderUrl = base + '/order/delete.jhtml'; //删除订单
    oOrder.commentUrl = base + '/member/comment/list.jhtml'; //评价
    oOrder.refundUrl = base + '/refund/record.jhtml'; //申请退款
    oOrder.refundDetailUrl = base + '/refund/record.jhtml'; //退款详情

    //订单列表页面下启用
    if ($('#order-list').length) {
        new OrderController($('#order-list'), oOrder, new OrderListEvent());
    }

    //订单详情页面下启用
    if ($('#order-detail').length) {
        new OrderController($('#order-detail'), oOrder, new OrderDetailEvent());
    }
});
